//
//  ChooseRoomTypeViewController.m
//  ZhuanZuBao
//
//  Created by 戴伟 on 2020/9/10.
//  Copyright © 2020 戴伟. All rights reserved.
//

#import "ChooseFloorViewController.h"
#import "PickerViewCell.h"

@interface ChooseFloorViewController ()<UIPickerViewDelegate, UIPickerViewDataSource>

@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (weak, nonatomic) IBOutlet UIView *contentView;

@end

@implementation ChooseFloorViewController

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(0);
    }];
    
    [UIView animateWithDuration:0.4 animations:^{
        [self.view layoutIfNeeded];
        self.view.backgroundColor = [UIColor colorWithHexString:@"#000000" andAlpha:0.5];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"#000000" andAlpha:0];
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(225);
    }];
}

- (IBAction)cancel:(id)sender {
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(225);
    }];
    
    [UIView animateWithDuration:0.4 animations:^{
        [self.view layoutIfNeeded];
        self.view.backgroundColor = [UIColor colorWithHexString:@"#000000" andAlpha:0];
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion: nil];
    }];
}

- (IBAction)confirm:(id)sender {
    NSInteger index1 = [_pickerView selectedRowInComponent:0];
    NSInteger index2 = [_pickerView selectedRowInComponent:1];
    NSString *floor = @"";
    if (index1 == 0) {
        floor = @"-2层";
    }else if (index1 == 1){
        floor = @"-1层";
    }else{
        floor = [NSString stringWithFormat:@"%ld层", index1-1];
    }
    if (self.selectFloorBlock) {
        self.selectFloorBlock([NSString stringWithFormat:@"%@ 共%ld层", floor, index2+1 ]);
    }
    [self cancel:nil];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return 32;
    }
    return 30;
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 44;
}

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    PickerViewCell *cell = (PickerViewCell *)view;
    if (!cell) {
        cell = [[PickerViewCell alloc] initWithFrame:CGRectMake(0, 0, DEVICE_SCREEN_WIDTH, 44)];
    }
    if (component == 0) {
        if (row == 0) {
            cell.textLabel.text = @"-2层";
        }else if(row == 1){
            cell.textLabel.text = @"-1层";
        }else{
            cell.textLabel.text = [NSString stringWithFormat:@"%ld层", row-1];
        }
        
    }else if(component == 1){
        cell.textLabel.text = [NSString stringWithFormat:@"共%ld层", row+1];
    }
    return cell;
}


@end
